<template>
    <div>
        <div class="permissMana" >
            <el-input  size="small" placeholder="请输入英文角色名" v-model="role.name">
                <template slot="prepend">ROLE_</template>
            </el-input>
            <el-input size="small" placeholder="请输入中文角色名" v-model="role.nameZh" @keydown.enter.native="doAddRole"></el-input>
            <el-button type="primary" size="small" icon="el-icon-plus" @click="doAddRole">添加角色</el-button>
        </div>
        <div class="permissManaMain">
            <el-collapse accordion @change="change" v-model="showName">
                <el-collapse-item :title="r.nameZh" :name="r.id" v-for="(r,index) in roles" :key="index">
                    <el-card class="box-card">
                        <div slot="header" class="clearfix">
                            <span>可访问的资源</span>
                            <el-button style="float: right; padding: 3px 0;color: #ff0000" icon="el-icon-delete" type="text" @click="deleteRole(r.id,r.nameZh)"></el-button>
                        </div>
                        <div>
                            <el-tree
                                    show-checkbox
                                    node-key="id"
                                    :key="index"
                                    ref="tree"
                                    :default-checked-keys="menuidsbyroleid"
                                    :data="allmenus" :props="defaultProps"></el-tree>
                            <div style="display:flex;justify-content:flex-end">
                                <el-button size="small" @click="cancelUpdate">取消修改</el-button>
                                <el-button size="small" type="primary" @click="doUpdate(r.id,index)">确认修改</el-button>
                            </div>
                        </div>
                    </el-card>
                </el-collapse-item>
            </el-collapse>
        </div>
    </div>
</template>

<script>
    export default {
        name: "PermissMana",
        data() {
            return{
                /*rules:{
                    name:[{required:true,message:"请输入角色英文名",trigger:"blur"}],
                    nameZh:[{required:true,message:"请输入角色中文名",trigger:"blur"}],
                },*/
                showName:-1,
                menuidsbyroleid:[],
                role:{
                    name:'',
                    nameZh:''
                },
                //查询所有角色
                roles:[],
                allmenus:[],
                //defaultProps 属性作用定义key的名字
                //如果返回的 children 和label 可以不定义defaultProps
                defaultProps: {
                    children: 'children',
                    label: 'name'
                }
            }
        },
        mounted() {
            this.inntPermiss();
        },
        methods: {
            deleteRole(rid,name) {
                this.$confirm('此操作将永久删除该【'+name+'】角色, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    //执行删除操作
                    this.deleteRequest("/system/basic/permiss/"+rid).then(resp =>{
                        if (resp) {
                            this.inntPermiss();
                        }
                    })
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });
            },
            doAddRole() {
                if (this.role.name && this.role.nameZh) {
                    this.role.name = "ROLE_"+this.role.name;
                    console.log("中文名"+this.role.name);
                    this.postRequest("/system/basic/permiss/",this.role).then(resp =>{
                        if (resp) {
                            this.inntPermiss();
                        }
                    })
                } else {
                    this.$message.error("不能有空值")
                }

            },
            cancelUpdate() {
                this.showName = -1;
            },
            doUpdate(rid,index) {
                console.log(rid);
                let tree = this.$refs.tree[index];
                //只选择叶子节点
                let checkedKeys = tree.getCheckedKeys(true);
                console.log(checkedKeys);
                let params = {
                    rid:rid,
                    menuids:checkedKeys
                }
                let url = "/system/basic/permiss/update?rid="+rid;
                checkedKeys.forEach(key =>{
                    url += "&menuids="+key;
                })
                //拼接后 /system/basic/permiss/update?rid=1&menuids=23&menuids=24
                this.putRequest(url).then(resp =>{
                    if (resp) {
                        this.inntPermiss();
                        this.showName = -1;
                    }
                })
            },
            //点击角色时，回调函数
            change(name) {
                if (name) {
                    this.initAllMenus();
                    this.inntMenuidsByid(name);
                }
            },
            //初始化加载所有资源
            initAllMenus() {
                this.getRequest("/system/basic/permiss/menuall").then(resp =>{
                    if (resp) {
                        this.allmenus = resp.obj;
                    }
                })
            },
            //查询角色所拥有的角色名
            inntMenuidsByid(rid) {
                this.getRequest("/system/basic/permiss/menuidsbyroleid/"+rid).then(resp =>{
                    if (resp) {
                        this.menuidsbyroleid = resp.obj;
                    }
                })
            },
            inntPermiss() {
                this.getRequest("/system/basic/permiss/").then(resp =>{
                    if (resp) {
                        this.roles = resp.obj;
                    }
                })
            }
        }
    }
</script>

<style>

    .permissMana {
        display: flex;
        justify-content: flex-start;
    }
    .permissMana .el-input {
        width: 300px;
        margin-right: 3px;
    }
    .permissManaMain {
        margin-top: 10px;
        width: 700px;
    }
</style>